//
//  QAPublishViewController.m
//  BaseProject
//
//  Created by 王洋 on 2020/12/11.
//  Copyright © 2020 王洋. All rights reserved.
//
#import "QuestionAreaViewController.h"

#import "QAPublishViewController.h"

@interface QAPublishViewController ()<UITextViewDelegate>

@property (weak, nonatomic) IBOutlet UIButton *qaBtn;

@property (weak, nonatomic) IBOutlet UITextField *titleTF;

@property (weak, nonatomic) IBOutlet UITextView *contentTV;

@property (weak, nonatomic) IBOutlet UIView *relateQAControl;

//领域id
@property (nonatomic, copy) NSString *paId;

@end

@implementation QAPublishViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self initView];
}

- (void)initView {
    
    self.title = @"提问";
    self.view.backgroundColor = UIColorHex(0xffffff);

    UIButton *publishBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    publishBtn.frame = CGRectMake(0,0,52,24);
    publishBtn.layer.cornerRadius = 12.0f;
    [publishBtn setBackgroundColor:UIColorHex(0xFF4242)];
    publishBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [publishBtn setTitle:@"发布" forState:UIControlStateNormal];
    [publishBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [publishBtn addTarget:self action:@selector(publishAction) forControlEvents:UIControlEventTouchUpInside];
    
    [publishBtn.widthAnchor constraintEqualToConstant:52].active =YES;
    [publishBtn.heightAnchor constraintEqualToConstant:24].active =YES; 
    
    UIBarButtonItem *publishItem = [[UIBarButtonItem alloc] initWithCustomView:publishBtn];

    self.navigationItem.rightBarButtonItems =@[publishItem];
    
    self.contentTV.delegate = self;
    
    [self initViewAction];
}

//仿placeholder
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
    if ([textView.text isEqualToString:@"请详细补充问题描述，越详细越容易得到回答哦"]) {
        textView.textColor = UIColorHex(0x333333);
        textView.text = @"";
    }
    return YES;
}

- (BOOL)textViewShouldEndEditing:(UITextView *)textView {
    if ([textView.text isEqualToString:@""]) {
        textView.textColor = UIColorHex(0xbbbbbb);
        textView.text = @"请详细补充问题描述，越详细越容易得到回答哦";
    }
    return YES;
}

- (void)initViewAction {
    
    self.relateQAControl.userInteractionEnabled = YES;
    WEAKSELF
    [self.relateQAControl addGestureTapEventHandle:^(id  _Nonnull sender, UITapGestureRecognizer * _Nonnull gestureRecognizer) {
        QuestionAreaViewController *vc = [[QuestionAreaViewController alloc] init];
        
        vc.QuestionAreaBlock = ^(CommunityAreaModel * _Nonnull model) {
            
            self.paId = model.paId;
            [self.qaBtn setTitle:model.fieldName forState:UIControlStateNormal];
            self.qaBtn.titleLabel.font = [UIFont systemFontOfSize:12];
            [self.qaBtn setTitleColor:UIColorHex(0xFF4242) forState:UIControlStateNormal];
            [self.qaBtn setBackgroundColor:UIColorHex(0xFFF0F0)];
        };
        

        [weakSelf.navigationController pushViewController:vc animated:YES];
    }];
    
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.qaBtn pp_clipCorners:UIRectCornerAllCorners radius:self.qaBtn.frame.size.height/2];
    });
}

- (void)publishAction {
    
    if (self.paId.length > 0) {
        
        if (self.titleTF.text.length >= 5 && self.titleTF.text.length <= 20) {
            
            if (![self.contentTV.text isEqualToString:@"请详细补充问题描述，越详细越容易得到回答哦"] && self.contentTV.text.length > 0) {
                [self request_publishPost];
            } else {
                [SVProgressHUD showErrorWithStatus:@"请输入问题描述"];
                [SVProgressHUD dismissWithDelay:1];
            }
        } else {
            [SVProgressHUD showErrorWithStatus:@"问题为5~20字"];
            [SVProgressHUD dismissWithDelay:1];
        }
        
    } else {
        [SVProgressHUD showErrorWithStatus:@"请关联问题所属领域"];
        [SVProgressHUD dismissWithDelay:1];
    }

}

//
- (void)request_publishPost {
        
    NSMutableDictionary *dic = @{}.mutableCopy;

    [dic setValue:self.contentTV.text forKey:@"content"];
    [dic setValue:self.titleTF.text forKey:@"title"];
    
    [dic setValue:self.paId forKey:@"paId"];

    //类型（1：谈婚-帖子，2：论嫁-话题） 必传
    [dic setValue:@"1" forKey:@"type"];
    [dic setValue:@"2" forKey:@"source"];

    [dic setValue:[TTUserModel sharedObject].userId forKey:@"userId"];
    
    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,API_QAPublish];
    
    WEAKSELF
    [SVProgressHUD showWithStatus:@"正在发布"];
    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:dic FinishBlock:^(id  _Nonnull data) {
        
        [SVProgressHUD dismiss];
        if ([data isKindOfClass:[NSDictionary class]]) {
            //请求成功
            NSInteger code = [[data valueForKey:@"code"] integerValue];
            if (code == 10000) {

                [weakSelf.navigationController popViewControllerAnimated:YES];
            } else {
                [SVProgressHUD showErrorWithStatus:[data valueForKey:@"message"]];
                [SVProgressHUD dismissWithDelay:1];
            }
        }
        
    } FailedBlock:^(NSError * _Nonnull error) {
        [SVProgressHUD dismiss];
    }];
}

@end
